package com.cj.mobile.iabp.mvp.presenter;

import static com.cj.mobile.iabp.BuildConfig.IS_DEBUG_DATA;

import android.os.Bundle;

import com.cj.mobile.iabp.app.config.CommonRetryWithDelay;
import com.cj.mobile.iabp.app.utils.RxUtils;
import com.cj.mobile.iabp.mvp.contract.PatientInfoContract;
import com.cj.mobile.iabp.mvp.model.entity.ArchiveInfo;
import com.cj.mobile.iabp.mvp.model.entity.PatientDetail;
import com.cj.mobile.iabp.mvp.model.entity.PatientInfo;
import com.cj.mobile.iabp.mvp.model.entity.RemoveInfo;
import com.cj.mobile.iabp.mvp.model.entity.RoundsInfo;
import com.cj.mobile.iabp.mvp.model.entity.SurgeryInfo;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2025/09/24 15:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class PatientInfoPresenter extends BasePresenter<PatientInfoContract.Model, PatientInfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;

    // 患者ID
    private String id;

    @Inject
    public PatientInfoPresenter(PatientInfoContract.Model model, PatientInfoContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
    }

    /**
     * 接收外面传过来的参数
     */
    public void getBundleValues(Bundle bundle) {
        // 获取默认值
        if (bundle != null) {
            id = bundle.getString("id", "");
        }
    }

    public void initData() {
        getPatientDetail();
    }

    /**
     * 获取患者详情
     */
    public void getPatientDetail() {
        if (IS_DEBUG_DATA) {
            // 基本信息
            PatientInfo patient = new PatientInfo("2025-08-11 10:12:19", "2025-07-26 00:24:35",
                    "", "", "",
                    "0", "", "79", "60889178", "1", "姜文成", "主二", "61", "1", "177",
                    "80", "23", "主动脉外科中心二区", "2", "朝阳院区", "HF", "",
                    "冠心病", "2", "1", "i5", "37", "", "", "33c9591de1cfba80dfc98facac0c265f",
                    "", "");

            // 手术信息
            SurgeryInfo surgery = new SurgeryInfo("2025-07-26 08:11:25", "2025-07-26 00:26:49",
                    "33c9591de1cfba80dfc98facac0c265f", "33c9591de1cfba80dfc98facac0c265f", "0", "0", "",
                    "76", "79", "33c9591de1cfba80dfc98facac0c265f", "23", "2", "60889178", "姜文成",
                    "7", "工作日", "", "OPCABG", "2025-07-24 08:27", "2025-07-25 23:30",
                    "安琪", "张梓榕", "", "37", "52", "", "", "", "", "", "Arrow", "7.5F", "40ml", "左侧股动脉",
                    "否", "https://iabp.wecarenovo.com/img/33c9591de1cfba80dfc98facac0c265f/2025072600/4tmp_eeFPFc.jpg", "", "轻", "无", "", "无", "有", "术后", "被迫", "无",
                    "ICU", "搭桥术后1天，心律失常", "房颤", "", "150", "100/50", "房颤", "", "50", "110/60", "5", "5", "", "", "", "", "", "5", "5", "", "", "", "", "", "", "", "",
                    "21", "26", "112", "60.77", "629", "1.2", "1200",
                    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "双下肢多发斑块", "1");
            // 查房信息
            List<RoundsInfo> rounds = new ArrayList<>();
            rounds.add(new RoundsInfo("","2025-07-30 08:43:49","fc3bc8c6b6ac0d7d1f086245733eded7","","0","0","","137","","",
                    "i5","79","37","李雨馨","","","2025-07-30 08:43","","","","","皮温凉,足背不可触及","皮温凉,足背不可触及,下肢发花","","","7","足背未及，皮凉，右足发花，双足运动好",
                    "2025-07-29","27","26","174.5","35.55","226.6","2.4","44.3","1","52","40","","",""));
            rounds.add(new RoundsInfo("","2025-07-31 13:13:54","33c9591de1cfba80dfc98facac0c265f","","0","0","","141","","",
                    "i5","79","37","安琪","","","2025-07-31 13:13","","","","","足背不可触及,未见发花,CRT>3s","足背不可触及,下肢发花,CRT>3s","无","","7","右足趾花斑",
                    "2025-07-30","48","39","161.4","39.02","221.4","2.1","32.9","1","","","","",""));
            rounds.add(new RoundsInfo("","2025-08-01 19:38:56","ebeefa95219c9176bd91ec4a8f2681fe","","0","0","","146","","",
                    "i5","79","37","宋诗咏","","","2025-08-01 10:40","","","","","","","","","7","右足趾散在紫绀，足背动脉未触及，未诉疼痛，肌力肌张力正常",
                    "","","","","","","","","1","","","","",""));

            // 撤机信息
            RemoveInfo remove = new RemoveInfo("2025-08-11 10:12:19","2025-07-30 08:40:52","fc3bc8c6b6ac0d7d1f086245733eded7","","1","0","","77",
                    "","","是","2025-07-29 20:35:00","93小时","否","存活", "79","无","","","","","","","","","","","","","","",
                    "1","","","");
            // 归档信息
            ArchiveInfo archive = new ArchiveInfo("2025-08-05 15:41:02","2025-08-05 09:20:28","33c9591de1cfba80dfc98facac0c265f","","0","0","",
                    "39","79","33c9591de1cfba80dfc98facac0c265f","","","","","是","存活","26.2","54","39","冠心病","心功能IV级 脑梗死 肾损伤","","","","35","0","","");

            mRootView.loadPatientDetail(new PatientDetail("7", patient, surgery, rounds, remove, archive));
        } else {
            mModel.getPatientDetail(id)
                    .compose(RxUtils.applySchedulers(mRootView))                                    // 使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<PatientDetail>(mErrorHandler) {
                        @Override
                        public void onNext(PatientDetail info) {
                            mRootView.loadPatientDetail(info);
                        }
                    });
        }

    }

    /**
     * 删除查房信息
     * @param id    记录id
     */
    public void removeRounds(String id) {
        if (IS_DEBUG_DATA) {
            getPatientDetail();
        } else {
            mModel.removeRounds(id, "1")
                    .subscribeOn(Schedulers.io())
                    .retryWhen(new CommonRetryWithDelay(2, 2))    // 遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                    .doOnSubscribe(disposable -> {
                        mRootView.showLoadingSubmit();
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(() -> {
                        mRootView.hideLoadingSubmit();
                    }).compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                    .subscribe(new ErrorHandleSubscriber<Integer>(mErrorHandler) {

                        @Override
                        public void onNext(Integer commonResponse) {
                            mRootView.showMessage("删除查房信息成功！");
                            getPatientDetail();
                        }
                    });
        }
    }
}